/*
 * Copyright (c) 2009, 2011, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef ALKES_TVECTOR3_H_INCLUDED_
#define ALKES_TVECTOR3_H_INCLUDED_

#include "alkes/internal.h"
#include "alkes/math/TVector2.h"
#include <math.h>

namespace alkes {

template<typename T>
struct TVector3 {
    typedef T   value_type;

    const static TVector3<T> ZERO;
    const static TVector3<T> ONE;

    T   x;
    T   y;
    T   z;

    operator TVector2<T>&() { return *reinterpret_cast<TVector2<T>*>(this); }
    operator const TVector2<T>&() { return *reinterpret_cast<const TVector2<T>*>(this); }

    bool operator==(const TVector3& rh) const;
    bool operator!=(const TVector3& rh) const
    {
        return !(*this == rh);
    }

    void normalize();
    const TVector3 cross(const TVector3& rh) const;
};

template<typename T>
const TVector3<T> TVector3<T>::ZERO = {
    (T)0,
    (T)0,
    (T)0
};
template<typename T>
const TVector3<T> TVector3<T>::ONE = {
    (T)1,
    (T)1,
    (T)1
};

template<typename T>
bool TVector3<T>::operator==(const TVector3<T>& rh) const
{
    return x == rh.x && y == rh.y && z == rh.z;
}

template<typename T>
void TVector3<T>::normalize()
{
    float len = sqrtf(x * x + y * y + z * z);
    x = (T)(x / len);
    y = (T)(y / len);
    z = (T)(z / len);
}

template<typename T>
const TVector3<T> TVector3<T>::cross(const TVector3& rh) const
{
    TVector3<T> ret = {
        y * rh.z - rh.y * z,
        z * rh.x - rh.z * x,
        x * rh.y - rh.x * y
    };
    return ret;
}

}

#define TYPE    alkes::TVector3<T>

#define DEFINE_OP(op)                                       \
    template<typename T>                                    \
    const TYPE operator op(const TYPE& lh, const TYPE& rh)  \
    {                                                       \
        TYPE v = {lh.x op rh.x, lh.y op rh.y, lh.x op rh.z};\
        return v;                                           \
    }

DEFINE_OP(+)
DEFINE_OP(-)
DEFINE_OP(*)
DEFINE_OP(/)

#undef DEFINE_OP
#define DEFINE_OP(op)                                           \
    template<typename T, typename U>                            \
    const TYPE operator op(const TYPE& lh, const U rh)          \
    {                                                           \
        TYPE v = {T(lh.x op rh), T(lh.y op rh), T(lh.z op rh)}; \
        return v;                                               \
    }

DEFINE_OP(+)
DEFINE_OP(-)
DEFINE_OP(*)
DEFINE_OP(/)

#undef DEFINE_OP
#define DEFINE_OP(op)                                           \
    template<typename T, typename U>                            \
    const TYPE operator op(const U lh, const TYPE& rh)          \
    {                                                           \
        TYPE v = {T(lh op rh.x), T(lh op rh.y), T(lh op rh.z)}; \
        return v;                                               \
    }

DEFINE_OP(+)
DEFINE_OP(-)
DEFINE_OP(*)
DEFINE_OP(/)

#undef DEFINE_OP
#define DEFINE_OP(op)                                       \
    template<typename T>                                    \
    const TYPE& operator op(TYPE& lh, const TYPE& rh)       \
    {                                                       \
        lh.x op rh.x; lh.y op rh.y; lh.z op rh.z;           \
        return lh;                                          \
    }

DEFINE_OP(+=)
DEFINE_OP(-=)
DEFINE_OP(*=)
DEFINE_OP(/=)

#undef DEFINE_OP
#define DEFINE_OP(op)                                       \
    template<typename T, typename U>                        \
    const TYPE& operator op(TYPE& lh, const U rh)           \
    {                                                       \
        lh.x op rh; lh.y op rh; lh.z op rh;                 \
        return lh;                                          \
    }

DEFINE_OP(+=)
DEFINE_OP(-=)
DEFINE_OP(*=)
DEFINE_OP(/=)

#undef DEFINE_OP
#undef TYPE

#endif
